var lianxiren = lianxiren.lianxiren;
for(var i in lianxiren) { //汉字转拼音
	lianxiren[i].pinyin = pinyinUtil.getFirstLetter(lianxiren[i].n);
}
//排序
for(var i = 0; i < lianxiren.length - 1; i++) {
	for(var k = 0; k < lianxiren.length - 1 - i; k++) {
		n = lianxiren[k];
		if(lianxiren[k].pinyin > lianxiren[k + 1].pinyin) {
			lianxiren[k] = lianxiren[k + 1];
			lianxiren[k + 1] = n;
		}
	}
}
var shouzimu = new Array();
for(var j in lianxiren) { //汉字转拼音
	shouzimu += lianxiren[j].pinyin[0];
}

var szu = new Array();
for(var ss = 0; ss < shouzimu.length; ss++) {
	if(szu.indexOf(shouzimu[ss]) >= 0) {
		$("#leflist_list2_ul3").append('<div id="leflist_list2_ul3_' + ss + '" class="leflist_list2_ul3_li">' + lianxiren[ss].t + lianxiren[ss].n + '</div>')
		$("#liaoxian_xianxiren").append('<div id="liaoxian_xianxiren_' + ss + '" class="liaoxian_xianxiren_list">' + '<span></span>' + lianxiren[ss].t + '<span>' + lianxiren[ss].n + '</span>' + '</div>')
	} else {
		szu += shouzimu[ss];
		$("#leflist_list2_ul3").append('<div class="leflist_list2_ul3_title">' + shouzimu[ss] + '</div>');
		$("#leflist_list2_ul3").append('<div id="leflist_list2_ul3_' + ss + '" class="leflist_list2_ul3_li">' + lianxiren[ss].t + lianxiren[ss].n + '</div>')
		$("#liaoxian_xianxiren").append('<div class="liaoxian_xianxiren_title">' + shouzimu[ss] + '</div>');
		$("#liaoxian_xianxiren").append('<div id="liaoxian_xianxiren_' + ss + '" class="liaoxian_xianxiren_list">' + '<span></span>' + lianxiren[ss].t + '<span>' + lianxiren[ss].n + '</span>' + '</div>')
	}
}

//以下为联系人列表
$("#leflist_list2_ul3 .leflist_list2_ul3_li").each(function() {
	var num = 0;
	$(this).click(function() {
		for(var t = 0; t < $(".leflist_list2_ul3_li").length; t++) {
			if($(this).attr("id") == "leflist_list2_ul3_" + t) {
				num = t;
			}
		}
		$(".leflist_list2_ul3_li").css("background-color", "#2E3238");
		$(this).css("background-color", "#3A3F45");
		$id = $(this).attr("id");
		$("#right_title").html($(this).text());
		var $main = $("#right_main_3");
		$main.children().hide();

		$this = $("#" + $id + "_main");
		if($this.length > 0) {
			$this.show();
		} else {
			$main.append('<div id="' + $id + '_main"><div class="right_main_3_touxiang">' + lianxiren[num].t + '</div><div class="right_main_3_name">' + lianxiren[num].n + '<img src="img/头像图标.png" /></div><div class="right_main_3_nicheng">' + lianxiren[num].wxname + '</div><div class="right_main_3_beizhu">备注：' + lianxiren[num].n + '</div><div class="right_main_3_diqu">地区：' + lianxiren[num].diqu + '</div><input class="right_main_3_faxiaoxi" type="button" value="发消息"/></div> ');
			$("#" + $id + "_main .right_main_3_faxiaoxi").click(function() {
				$main.hide();
				$("#leflist_list1_liaotian").click();

				if($("#leflist_list2_" + num).length > 0) {
					$("#leflist_list2_ul1").prepend($("#leflist_list2_" + num))
					$("#leflist_list2_" + num).click();
				} else {
					$("#leflist_list2_ul1").prepend("<li id='leflist_list2_" + num + "'><span>" + lianxiren[num].t + "</span><p class='name'><span>" + lianxiren[num].n + "</span><span class='span_time'></span></p><p class='liaotian'></p></li>");
					//添加点击事件
					$("#leflist_list2_" + num).click(li1tianjia);
					$("#leflist_list2_" + num).click();
				}
			})
		}
	})
})

function li1tianjia() {
	$("#right_img").hide();
	$("#right_title").html($(this).children().eq(1).children().eq(0).html());
	var $main = $("#right_main_1");
	$("#leflist_list2_ul1 li").css("background-color", "#2E3238");
	$(this).css("background-color", "#3A3F45");
	$main.children().css("display", "none");
	$id = $(this).attr("id");
	if($("#" + $id + "_main").length > 0) {
		$("#" + $id + "_main").show();
	} else {
		$main.append('<div id="' + $id + '_main"><div class="right_xianshi"></div><div class="right_zhushou"><img src="img/图标/表情.png" class="biaoqing"/><img src="img/图标/剪切.png" /><img src="img/图标/文件.png" /></div><div class="right_text" contenteditable="true" onkeyup="backhome(event)"></div><p><input type="button" value="发送" class="right_fasong" /><span class="right_zhushi">按下Ctrl+Enter发送</span> </p></div>')
			//点击表情图片、、、、、、、、、、、、、、、、、、
		$("#" + $id + "_main>.right_zhushou>.biaoqing").click(function() {
			//添加图片、、、、、、、、、、、、、、、、、、
			if($("#" + $id + "_main>.right_xianshi .bq").length > 0) {
				var $display = $("#" + $id + "_main>.right_xianshi .bq").css("display") == "none" ? "block" : "none";
				$("#" + $id + "_main>.right_xianshi .bq").css("display", $display);
				//							console.log(1);
			} else {
				//							console.log(2);
				$("#" + $id + "_main>.right_xianshi").append("<div class='bq'><div class='tupian'>图片</div><div class='biao_qing'>表情</div><div class='cle'></div><div class='tupian_main'><img src='img/webwxgeticon (1).jpg' /><img src='img/webwxgeticon (2).jpg' /></div><div class='biaoqing_main'></div></div>");
				$("#" + $id + "_main>.right_xianshi .bq img").click(function() {
						$this = this.outerHTML;
						xianshi += $this;
						cli();
					})
					//添加表情、、、、、、、、、、、、、
				var biaolist = biaoqing.biaoqing;
				for(var i in biaolist) {
					var $biao = $("#" + $id + "_main>.right_xianshi .bq .biaoqing_main")
					$biao.append(biaolist[i].title);
					$biao.children().eq(i).css("width", "20")
				}
				//发送表情到文本框
				var $biaoqingmain = "#" + $id + "_main>.right_xianshi .bq .biaoqing_main img"
				$($biaoqingmain).click(function() {
						$("#" + $id + "_main [class=right_text]").append(this.outerHTML);
					})
					//图片和表情的切换
				var $bq = $("#" + $id + "_main>.right_xianshi .bq");
				$bq.children().eq(0).click(function() {
					$bq.children().eq(0).css("background-color", "white");
					$bq.children().eq(1).css("background-color", "#D6D6D6");
					$bq.children().eq(3).show();
					$bq.children().eq(4).hide();
				})
				$bq.children().eq(1).click(function() {
					$bq.children().eq(1).css("background-color", "white");
					$bq.children().eq(0).css("background-color", "#D6D6D6");
					$bq.children().eq(4).show();
					$bq.children().eq(3).hide();
				})
			}

		});
	}
	$(this).children().eq(2).css("color", "white");
	img = $(this).children().eq(0).html();
	txt = $("#" + $id + "_main [class=right_text]")[0];
	info = $("#" + $id + "_main [class=right_xianshi]");
	$time = $(this).children().eq(1).eq(0).children().eq(1);
	$liao = $(this).children().eq(2);
}

$("#right_main_1").click(function(event) {
	var $this = event.target ? event.target : event.srcElement;
	if($this.type == "button") {
		cli();
	}
});
var xianshi = "";

function cli() {
	var num = parseInt(Math.random() * 4) + 1;
	xianshi += txt.innerHTML;
	//右
	info.append("<div class='time'>" + getTime() + "</div>"); //时间
	info.append("<div class='rright'>" + $("#leflist_title").children().eq(0).html() + "</div>") //头像
	info.append("<div class='rleft'><div class='arrow2'></div><div class='neirong'>" + xianshi + "</div></div>")
	info.append("<div class='cle'></div>")
		//左
	$.post("http://www.tuling123.com/openapi/api", {
		key: "26c463640125432b9f275ffe6d999c05",
		info: xianshi,
		userid: "12345678"
	}, function(data) {
		//机器人返回的信息
		info.append("<div class='time'>" + getTime() + "</div>"); //时间
		info.append("<div class='lleft'>" + img + "</div>"); //头像
		if(data.url) {
			info.append("<div class='lright'><div class='arrow1'></div><div class='neirong'>" + data.text + "<a target='_blank' href='" + data.url + "'>打开页面</a>" + "</div></div>");
		} else if(data.list) {
			info.append('<div class="lright">' + '<div class="xinwen"><div class="xinwen_1"><a target="_blank" href="' + data.list[0].detailurl + '"><p>' + data.list[0].article + '</p></a><a target="_blank" href="' + data.list[0].detailurl + '"><img src="' + data.list[0].icon + '" /></a></div><div class="xinwen_2"><a target="_blank" href="' + data.list[1].detailurl + '"><div class="mark">' + data.list[1].article + '</div></a><a target="_blank" href="' + data.list[1].detailurl + '"><img src="' + data.list[1].icon + '" /></a></div><div class="xinwen_2"><a target="_blank" href="' + data.list[2].detailurl + '"><div class="mark">' + data.list[2].article + '</div></a><a target="_blank" href="' + data.list[2].detailurl + '"><img src="' + data.list[2].icon + '" /></a></div><div class="xinwen_2"><a target="_blank" href="' + data.list[3].detailurl + '"><div class="mark">' + data.list[3].article + '</div></a><a target="_blank" href="' + data.list[3].detailurl + '"><img src="' + data.list[3].icon + '" /></a></div></div>' + '</div>');
		} else {
			info.append("<div class='lright'><div class='arrow1'></div><div class='neirong'>" + data.text + "</div></div>");
		}
		info.append("<div class='cle'></div>");
		info[0].scrollTop = info[0].scrollHeight;
		$lasthtml = $("#" + $id + "_main .lright>.neirong").last().html();
		$liao.html(imgpd.test($lasthtml) ? "图片" : $lasthtml);

	});
	$time.html(getTime());
	var imgpd = /^<img src/;
	$lasthtml = $("#" + $id + "_main .lright>.neirong").last().html();

	$liao.html(imgpd.test($lasthtml) ? "图片" : $lasthtml);
	txt.innerHTML = "";
	xianshi = "";
	info[0].scrollTop = info[0].scrollHeight;
}

function dd(num) {
	return num < 10 ? "0" + num : num;
}

function getTime() {
	var time = new Date();
	var hh = dd(time.getHours());
	var mm = dd(time.getMinutes());
	return hh + ":" + mm;
}

function backhome(event) {
	if(event.ctrlKey && !event.keyCode == 13) {
		txt.innerHTML = txt.innerHTML + '\n';
	}
}
window.onkeydown = function(event) {
	if(event.keyCode == 13 && event.ctrlKey) {
		cli();
	}
}

$("#leflist_title_list").click(function() {
	$("#leflist_title_list").css("display", "none")
})
$("#leflist_title_list li").eq(0).click(function() {
	$("#leflist_title_list_liaotian").show();
})

$("#leflist_title_list li").eq(1).click(function() {
	if($(this).attr("title") == "关闭桌面通知") {
		$(this).attr("title", "打开桌面通知")
		$(this).html("<img src='img/图标/桌面通知关.png' />打开桌面通知");
	} else {
		$(this).attr("title", "关闭桌面通知")
		$(this).html("<img src='img/图标/桌面通知开.png' />关闭桌面通知");
	}
})
$("#leflist_title_list li").eq(2).click(function() {
	if($(this).attr("title") == "关闭声音") {
		$(this).attr("title", "打开声音")
		$(this).html("<img src='img/图标/声音关.png' />打开声音");
	} else {
		$(this).attr("title", "关闭声音")
		$(this).html("<img src='img/图标/声音开.png' />关闭声音");
	}
})
$("#leflist_title_list li").eq(4).click(function() {
	if(confirm("是否确定离开")) {
		window.close();
	}
})
$("#leflist_title_xiala").click(function() {
		var $this = $("#leflist_title_list");
		if($this.css("display") == "none") {
			$this.css("display", "block");
		} else {
			$this.css("display", "none");
		}
	})
	//切换
$("#leflist_list1 p").each(function() {
	$(this).click(function() {
		$("#right_img").hide();
		$("#gundongttiao").css("display", "none");
		$("#gundongttiao1").css("display", "none");
		for(var i = 0; i < $("#leflist_list1 p").length; i++) {
			$("#leflist_list1 p").eq(i).children().eq(0).css("display", "none");
			$("#leflist_list2 ul").eq(i).css("display", "none");
			$("#leflist_list1 p").eq(i).children().eq(1).css("display", "block");
		}
		$(this).children().eq(0).css("display", "block");
		$(this).children().eq(1).css("display", "none");
		for(var j = 0; j < $("#leflist_list1 p").length; j++) {
			if($("#leflist_list1 p").eq(j).children().css("display") == "block") {
				$("#leflist_list2 ul").eq(j).css("display", "block");
				if(j == 0) {
					if($("#leflist_list2_ul1").css("height") > $("#leflist_list2").css("height")) {
						$("#gundongttiao").css("display", "block");
					}
				}
				if(j == 2) {
					if($("#leflist_list2_ul3").css("height") > $("#leflist_list2").css("height")) {
						$("#gundongttiao1").css("display", "block");
					}
				}
			}
		}
	})
})
$("#leflist_list1_liaotian").click(function() {
	$("#right_main_1").show();
	$("#right_main_2").hide();
	$("#right_main_3").hide();
})
$("#leflist_list1_zhong").click(function() {
	$("#right_main_2").show();
	$("#right_main_1").hide();
	$("#right_main_3").hide();
})
$("#leflist_list1_you").click(function() {
		$("#right_main_3").show();
		$("#right_main_2").hide();
		$("#right_main_1").hide();
	})
	//一下为滚动条
var m;
var y = 0;
var mtop = 0;
var hei = 0;
$("#gun").mousedown(function(event) {
	m = true;
	evt = event || window.event;
	Y = evt.clientY ? evt.clientY : evt.pageY;
	y = parseInt(Y);
	mtop = parseInt($("#gun").css("margin-top"));
	var stop = parseInt($("#leflist_list2_ul1").css("margin-top"));
	hei = parseInt($("#leflist_list2_ul1").height()) - parseInt($("#leflist_list2").height());
})
$(document).mousemove(function(event) {
		evt = event || window.event;
		if(evt.which == 1 && m == true) {
			Y1 = evt.clientY ? evt.clientY : evt.pageY;
			var y1 = parseInt(Y1);
			var juli = y1 - y;
			var top = mtop + juli;
			var t = parseInt($("#gundongttiao").height()) - parseInt($("#gun").height());
			if(top < 0) { //滚动条到达顶部
				top = 0;
			}
			if(top > t) { //滚动条到达底部
				top = t;
			}
			var stop1 = hei / t * top * -1;
			$("#gun").css("margin-top", top);
			$("#leflist_list2_ul1").css("margin-top", stop1);
		}
	}).mouseup(function() {
		m = false;
	})
	//一下为滚动条
var ms;
var ys = 0;
var smtop = 0;
var shei = 0;
$("#gun1").mousedown(function(event) {
	ms = true;
	evt = event || window.event;
	Y = evt.clientY ? evt.clientY : evt.pageY;
	ys = parseInt(Y);
	smtop = parseInt($("#gun1").css("margin-top"));
	var stop = parseInt($("#leflist_list2_ul3").css("margin-top"));
	shei = parseInt($("#leflist_list2_ul3").height()) - parseInt($("#leflist_list2").height());
})
$(document).mousemove(function(event) {
	evt = event || window.event;
	if(evt.which == 1 && ms == true) {
		Y1 = evt.clientY ? evt.clientY : evt.pageY;
		var ys1 = parseInt(Y1);
		var juli = ys1 - ys;
		var top = smtop + juli;
		var t = parseInt($("#gundongttiao1").height()) - parseInt($("#gun1").height());
		if(top < 0) { //滚动条到达顶部
			top = 0;
		}
		if(top > t) { //滚动条到达底部
			top = t;
		}
		var stop1 = shei / t * top * -1;
		$("#gun1").css("margin-top", top);
		$("#leflist_list2_ul3").css("margin-top", stop1);
	}
}).mouseup(function() {
	ms = false;
})

var listwen = list2.adv;
var $leflist_list2_ul2 = $("#leflist_list2_ul2");
for(var l in listwen) {
	$leflist_list2_ul2.append('<div id="leflist_list2_ul2_' + l + 't"><div class="leflist_list2_ul2_title">' + listwen[l].touxiang + '<span>' + listwen[l].name + '</span></div></div>')
	var listadv = listwen[l].adv;
	for(var p in listadv) {
		$("#leflist_list2_ul2_" + l + "t").append('<div class="leflist_list2_ul2_wen1" id="leflist_list2_ul2_wen1' + listadv[p].id + '"><span class="leflist_list2_ul2_wen1_1">' + listadv[p].info + '</span><span class="leflist_list2_ul2_wen1_2">' + listwen[l].touxiang + '</span></div>')
		$("#leflist_list2_ul2_wen1" + listadv[p].id).click(function() {
			var $id = $(this).attr("id");
			$("#right_main_2").children().hide();
			if($("#" + $id + "_ss").length > 0) {
				$("#" + $id + "_ss").show();
			} else {
				$("#right_main_2").append("<div class='wen_main' id='" + $id + "_ss'><iframe src='http://127.0.0.1:8020/javascript%e4%b8%8ecss/%e4%bd%9c%e4%b8%9a1/%e7%82%b9%e7%81%af%e6%b8%b8%e6%88%8f.html' width='1400' height='1400' scrolling='yes'></iframe></div>")
			}
			$(".right_title").html($("#" + $id + " .leflist_list2_ul2_wen1_1").html());
		})
	}
}

/*$(".leflist_list2_ul2_wen1").click(function() {
	$("#right_main_2").children().css("display", "none");
	$(".wen_main").css("display", "block");
	$("#right_title").html($(".leflist_list2_ul2_wen1_1").html());
});*/
$("#leflist_title_list_liaotian_title_guanbi").click(function() {
	$("#leflist_title_list_liaotian").hide();
});
$(".liaoxian_xianxiren_list span:even").css("float", "left").css("margin-top", 20).css("margin-left", 10).css("background-image", "url(img/图标/选中0.png)").css("width", 20).css("height", 22);
$(".liaoxian_xianxiren_list img").css("width", 40).css("margin-bottom", 5);
//联系人
var liannum = 0
$(".liaoxian_xianxiren_list").each(function() {
		var num = 0;
		$(this).click(function() {
			for(var t = 0; t < $(".liaoxian_xianxiren_list").length; t++) {
				if($(this).attr("id") == "liaoxian_xianxiren_" + t) {
					num = t;
				}
			}
			$(".liaoxian_xianxiren_list").css("background-color", "#FFFFFF");
			$(this).css("background-color", "#EFEFEF");
			var t = $(this).children().eq(1).get(0).outerHTML;
			var tt = t.replace(/[ ;/<>:""().=]/g, "");
			if($(this).children().eq(0).css("background-image") == 'url("http://127.0.0.1:8020/%E4%BA%8E%E6%B6%B5%2b%E7%BD%91%E9%A1%B5%E7%89%88%E5%BE%AE%E4%BF%A1/img/%E5%9B%BE%E6%A0%87/%E9%80%89%E4%B8%AD0.png")' || $(this).children().eq(0).css("background-image") == "url(http://127.0.0.1:8020/%E4%BA%8E%E6%B6%B5%2b%E7%BD%91%E9%A1%B5%E7%89%88%E5%BE%AE%E4%BF%A1/img/%E5%9B%BE%E6%A0%87/%E9%80%89%E4%B8%AD0.png)") {
				$(this).children().eq(0).css("background-image", "url(img/图标/选中1.png)");
				$("#liaoxian_xianxiren_t_sousuo").before(t);
				$("#liaoxian_xianxiren_t_tou img").last().attr("id", tt);
				liannum++;
			} else {
				$(this).children().eq(0).css("background-image", "url(img/图标/选中0.png)");
				$('#' + tt).remove();
				liannum--;
			}
			$queding = $("#liaoxian_xianxiren_queding");
			if(liannum > 0) {
				$queding.css("background-color", "#3CAF36");
				$queding.html("确定(" + liannum + ")");
			} else {
				$queding.css("background-color", "#C9C9C9");
				$queding.html("确定");
			}
		})
	})
	//联系人导入
$("#liaoxian_xianxiren_queding").click(function() {
		var num = 0;
		var arr = new Array();
		if(liannum > 0) {
			$("#leflist_list1_liaotian").click();
			$list = $("#liaoxian_xianxiren .liaoxian_xianxiren_list")
			for(var i = 0; i < $list.length; i++) {
				var $this = $list.eq(i);
				if($this.children().eq(0).css("background-image") == "url(http://127.0.0.1:8020/%E4%BA%8E%E6%B6%B5%2b%E7%BD%91%E9%A1%B5%E7%89%88%E5%BE%AE%E4%BF%A1/img/%E5%9B%BE%E6%A0%87/%E9%80%89%E4%B8%AD1.png)" || $this.children().eq(0).css("background-image") == 'url("http://127.0.0.1:8020/%E4%BA%8E%E6%B6%B5%2b%E7%BD%91%E9%A1%B5%E7%89%88%E5%BE%AE%E4%BF%A1/img/%E5%9B%BE%E6%A0%87/%E9%80%89%E4%B8%AD1.png")') {
					for(var t = 0; t < $(".leflist_list2_ul3_li").length; t++) {
						if($this.attr("id") == "liaoxian_xianxiren_" + t) {
							num = t;
							if($("#leflist_list2_" + num).length > 0) {
								$("#leflist_list2_ul1").prepend($("#leflist_list2_" + num))
								$("#leflist_list2_" + num).click();
							} else {
								$("#leflist_list2_ul1").prepend("<li id='leflist_list2_" + num + "'><span>" + lianxiren[num].t + "</span><p class='name'><span>" + lianxiren[num].n + "</span><span class='span_time'></span></p><p class='liaotian'></p></li>");
								//添加点击事件
								$("#leflist_list2_" + num).click(li1tianjia);
								$("#leflist_list2_" + num).click();
							}
						}
					}
				}
			}
			$("#leflist_title_list_liaotian").hide();
		}

	})
	//群聊列表导入
var qunliao = qunliao.qunliao;
for(var t = 0; t < qunliao.length; t++) {
	$("#liaoxian_qunliao").append('<div class="liaooxian_qunliao_list" id="liaooxian_qunliao_' + t + '"><div class="liaooxian_qunliao_list_1">' + qunliao[t].t + '</div><div class="liaooxian_qunliao_list_2"><h4>' + qunliao[t].n + '</h4></div><div class="cle"></div></div>')
}

$(".liaooxian_qunliao_list").each(function() {
		$(this).click(function() {
			var num = 0;
			for(var t = 0; t < $("#liaoxian_qunliao").children().length; t++) {
				if($(this).attr("id") == "liaooxian_qunliao_" + t) {
					num = t;
					if($("#leflist_list2_qun_" + num).length > 0) {
						$("#leflist_list2_ul1").prepend($("#leflist_list2_qun_" + num));
						$("#leflist_list2_qun_" + num).click();
					} else {
						$("#leflist_list2_ul1").prepend("<li id='leflist_list2_qun_" + num + "'><span>" + qunliao[num].t + "</span><p class='name'><span>" + qunliao[num].n + "</span><span class='span_time'></span></p><p class='liaotian'></p></li>");
						//添加点击事件
						$("#leflist_list2_qun_" + num).click(li1tianjia);
						$("#leflist_list2_qun_" + num).click();
					}
				}
			}
			$("#leflist_list1_liaotian").click();
			$("#leflist_title_list_liaotian").hide();
		})
	})
	//联系人点击事件
$("#liaoxian_xianxiren_").click(function() {
		$("#liaoxian_xianxiren_t").show();
		$("#liaoxian_qunliao").hide();
		$("#liaotian_xuanze_sanjiao1").show();
		$("#liaotian_xuanze_sanjiao2").hide();
	})
	//群聊点击事件
$("#liaoxian_qunliao_").click(function() {
	$("#liaoxian_xianxiren_t").hide();
	$("#liaoxian_qunliao").show();
	$("#liaotian_xuanze_sanjiao1").hide();
	$("#liaotian_xuanze_sanjiao2").show();
})